﻿//Developed by Augusto Vasconcelos - http://www.TibiaBots.Net
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Tibia;
using Tibia.Objects;
using Tibia.Util;
using Tibia.Constants;
using ComponentFactory.Krypton.Toolkit;
using System.Collections.Generic;

namespace LothusBot.Forms
{
    public partial class CaveBot : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public Item Pick = Items.Tool.Pick;
        public Item Rope = Items.Tool.Rope;
        public Item Shovel = Items.Tool.LightShovel;
        public string[] arrayWalker;

        Client c;
        Player p;
        public CaveBot()
        {
            InitializeComponent();
        }

        #region Eventos

        private void CaveBot_Load(object sender, EventArgs e)
        {
            c = Client.GetClients()[0];
            p = c.GetPlayer();
        }

        private void CaveBot_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Botões

        

        #endregion

        #region Timers

        private void timer_walker_Tick(object sender, EventArgs e)
        {
            if (lbWalker.SelectedItem.ToString() != "MoveN" && lbWalker.SelectedItem.ToString() != "MoveS" &&
                lbWalker.SelectedItem.ToString() != "MoveW" && lbWalker.SelectedItem.ToString() != "MoveE")
            {
                string[] chek = lbWalker.SelectedItem.ToString().Split('|');
                string pos = chek[1];
                string output = pos.Substring(pos.IndexOf("(") + 1, pos.IndexOf(")") - pos.IndexOf("(") - 1);
                arrayWalker = output.Split(',');
                if (p.Location == new Location(Convert.ToInt32(arrayWalker[0]),
                        Convert.ToInt32(arrayWalker[1]), Convert.ToInt32(arrayWalker[2]))
                        | p.Location.Z != Convert.ToInt32(arrayWalker[2]))
                {
                    if (lbWalker.SelectedIndex >= (lbWalker.Items.Count - 1))
                    {
                        lbWalker.SelectedIndex = 0;
                    }
                    else
                    {
                        lbWalker.SelectedIndex = lbWalker.SelectedIndex + 1;
                    }
                }
                if (chek[0] == "Walk")
                {
                    p.GoTo = new Location(Convert.ToInt32(arrayWalker[0]),
                        Convert.ToInt32(arrayWalker[1]), Convert.ToInt32(arrayWalker[2]));
                }
                else
                {
                    if (chek[0] == "Rope")
                    {
                        Location rope = new Location(Convert.ToInt32(arrayWalker[0]),
                        Convert.ToInt32(arrayWalker[1]), Convert.ToInt32(arrayWalker[2]));
                        Tile tile = c.Map.GetTile(rope);
                        c.Inventory.UseItemOnTile(Rope.Id, tile);
                        Thread.Sleep(500);
                    }
                    else
                    {
                        if (chek[0] == "Shovel")
                        {
                            Location shovel = new Location(Convert.ToInt32(arrayWalker[0]),
                            Convert.ToInt32(arrayWalker[1]), Convert.ToInt32(arrayWalker[2]));
                            Tile tile = c.Map.GetTile(shovel);
                            c.Inventory.UseItemOnTile(Shovel.Id, tile);
                            Thread.Sleep(500);
                            lbWalker.SelectedIndex = lbWalker.SelectedIndex + 1;
                        }
                    }
                }
            }
            else
            {
                if (lbWalker.SelectedItem.ToString() == "MoveN")
                {
                    p.Walk(Direction.Up);
                    Thread.Sleep(1000);
                    if (lbWalker.SelectedIndex >= (lbWalker.Items.Count - 1))
                    {
                        lbWalker.SelectedIndex = 0;
                    }
                    else
                    {
                        lbWalker.SelectedIndex = lbWalker.SelectedIndex + 1;
                    }
                }
                else
                {
                    if (lbWalker.SelectedItem.ToString() == "MoveS")
                    {
                        p.Walk(Direction.Down);
                        Thread.Sleep(1000);
                        if (lbWalker.SelectedIndex >= (lbWalker.Items.Count - 1))
                        {
                            lbWalker.SelectedIndex = 0;
                        }
                        else
                        {
                            lbWalker.SelectedIndex = lbWalker.SelectedIndex + 1;
                        }
                    }
                    else
                    {
                        if (lbWalker.SelectedItem.ToString() == "MoveE")
                        {
                            p.Walk(Direction.Right);
                            Thread.Sleep(1000);
                            if (lbWalker.SelectedIndex >= (lbWalker.Items.Count - 1))
                            {
                                lbWalker.SelectedIndex = 0;
                            }
                            else
                            {
                                lbWalker.SelectedIndex = lbWalker.SelectedIndex + 1;
                            }
                        }
                        else
                        {
                            if (lbWalker.SelectedItem.ToString() == "MoveW")
                            {
                                p.Walk(Direction.Left);
                                Thread.Sleep(1000);
                                if (lbWalker.SelectedIndex >= (lbWalker.Items.Count - 1))
                                {
                                    lbWalker.SelectedIndex = 0;
                                }
                                else
                                {
                                    lbWalker.SelectedIndex = lbWalker.SelectedIndex + 1;
                                }
                            }
                        }
                    }
                }
            }
        }

        #endregion

        #region CheckBox's

        private void chk_Bot_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_Bot.Checked)
            {
                timer_walker.Start();
            }
            else
            {
                timer_walker.Stop();
            }
        }

        #endregion

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lbWalker.Items.Add("Walk|" + p.Location.ToString());
            lbWalker.SelectedIndex = 0;
        }

        private void delPositionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lbWalker.Items.Count > 0)
            {
                lbWalker.Items.RemoveAt(lbWalker.SelectedIndex);
                if (lbWalker.Items.Count > 0)
                {
                    lbWalker.SelectedIndex = 0;
                }
            }
        }

        private void clearWaypointToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lbWalker.Items.Count > 0)
            {
                if (MessageBox.Show("Limpar a lista de waypoints?",
                        "Tem certeza?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    lbWalker.Items.Clear();
                }
            }
            else
            {
                MessageBox.Show("Lista vazia!");
            }
        }

        private void ropeItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lbWalker.Items.Add("Rope|" + p.Location.ToString());
            lbWalker.SelectedIndex = 0;
        }

        private void btpos_Save_Click(object sender, EventArgs e)
        {

        }

        private void westToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int x = p.Location.X - 1;
            int y = p.Location.Y;
            lbWalker.Items.Add("Shovel| (" +x+", "+y+", "+p.Location.Z.ToString()+")");
            lbWalker.SelectedIndex = 0;
        }

        private void eastToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int x = p.Location.X + 1;
            int y = p.Location.Y;
            lbWalker.Items.Add("Shovel| (" + x + ", " + y + ", " + p.Location.Z.ToString() + ")");
            lbWalker.SelectedIndex = 0;
        }

        private void northToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int x = p.Location.X;
            int y = p.Location.Y - 1;
            lbWalker.Items.Add("Shovel| (" + x + ", " + y + ", " + p.Location.Z.ToString() + ")");
            lbWalker.SelectedIndex = 0;
        }

        private void southToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int x = p.Location.X;
            int y = p.Location.Y + 1;
            lbWalker.Items.Add("Shovel| (" + x + ", " + y + ", " + p.Location.Z.ToString() + ")");
            lbWalker.SelectedIndex = 0;
        }

        private void northToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            lbWalker.Items.Add("MoveN");
            lbWalker.SelectedIndex = 0;
        }

        private void southToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            lbWalker.Items.Add("MoveS");
            lbWalker.SelectedIndex = 0;
        }

        private void eastToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            lbWalker.Items.Add("MoveE");
            lbWalker.SelectedIndex = 0;
        }

        private void westToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            lbWalker.Items.Add("MoveW");
            lbWalker.SelectedIndex = 0;
        }

        
    }
}
